// Copyright 2007-2008 Helsinki Institute for Information Technology
// and the authors.
//
// Authors: Harri Hämäläinen <hhamalai@iki.fi>

// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// AJAX request handler for Prototype Framework
// Parameters for diffent class methods are:
//  url - request url
//  callback - method to call if request was successful
//  value = value to send with the request

function HttpConnector() {}

HttpConnector.doGet = function(url, callback) {
    var options = { method: 'get' };
    options.asynchronous = !!callback;
    if (callback) { options.onSuccess = callback; }
    var request = new Ajax.Request(url, options);
    if (!callback) {
        return request.success() ? request.transport.responseText : null;
    }
}

HttpConnector.doPut = function(url, value, callback) {
    var options = {
        requestHeaders: { 'X-HTTP-Method-Override': 'PUT' },
        method: 'put',
        postBody: value,
        contentType: 'application/octet-stream',
        onFailure: function() { alert('Sorry, unauthorized.'); }
    };
    options.asynchronous = !!callback;
    if (callback) { options.onSuccess = callback; }
    var request = new Ajax.Request(url, options);
    if (!callback) {
        return request.success() ? request.transport.responseText : null;
    }
}

HttpConnector.doPost = function(url, value, callback) {
    var options = { 
        method: 'post',
        postBody: value,
        contentType: 'application/octet-stream',
        onFailure: function() { alert('Sorry, unauthorized.'); }
    };
    options.asyncronous = !!callback;
    if (callback) { options.onSuccess = callback; }
    var request = new Ajax.Request(url, options);
    if (!callback) {
    	return request.success() ? request.transport.responseText : null;
    }
}

HttpConnector.doDelete = function(url, callback) {
    var options = {
        requestHeaders: { 'X-HTTP-Method-Override': 'DELETE' },
        postBody: '',
        method: 'delete',
        onFailure: function() { alert('Sorry, unauthorized'); }
    };
    options.asynchronous = !!callback;
    if (callback) { options.onSuccess = callback; }
    new Ajax.Request(url, options);
}

// This one polls DB every polling_interval seconds and executes the callback for each successfull request 
HttpConnector.doPolling = function(url, last_mark, polling_interval, callback) {
    if (!callback)
        return;
    var last_mark = 0;  
    new PeriodicalExecuter(function()  {
        var requestUrl = url + '&last_mark='+last_mark;
        var response = new Ajax.Request(requestUrl, {
            method: 'get',
            onSuccess: function(transport) {
                var resp = transport.responseText.evalJSON(true);
                last_mark = resp['mark'];
                callback(resp);
            }
        });
    }, polling_interval);
}
